package com.zygh.policeasset.mapper;

import com.zygh.policeasset.pojo.ProjectSupervisor;
import com.zygh.policeasset.pojo.vo.ProjectFinalAccountsVO;
import com.zygh.policeasset.pojo.vo.ProjectSupervisorVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProjectSupervisorMapper {

    @Select({"<script>" +
            "select ps.*,p.name as projectName,af.app_status as appStatus," +
            "af.approval_steps_id as approvalStepsId,u.name as userName from project_supervisor ps " +
            "left join project p on ps.project_no = p.project_no " +
            "left join audit_flow af on ps.flow_no = af.flow_no " +
            " left join user u on af.applicant = u.id  "+
            "<where>" +
            "<if test='projectNo != null and projectNo.length &gt; 0'> ps.project_no = #{projectNo} </if>" +
            "<if test='time1 != null and time2 != null and time1.length &gt; 0 and time2.length &gt; 0'> and ps.create_time between #{time1} and #{time2} </if>" +
            "<if test='userId != null '> and af.applicant=#{userId} </if>" +
            "<if test=' status != null'> and af.app_status = #{status} </if>" +
            "</where>" +
            " order by ps.id desc" +
            "</script>"})
    @Results(id = "map", value = {
            @Result(property = "projectNo", column = "project_no"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "flowNo", column = "flow_no"),
            @Result(property = "sectionIds", column = "section_ids"),
            @Result(property = "winningAmount", column = "winning_amount"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")})
    List<ProjectSupervisorVO> findAll(@Param("status") Integer status,
                                      @Param("projectNo") String projectNo,
                                      @Param("time1") String time1,
                                      @Param("time2") String time2,
                                      @Param("userId") Integer userId);

    @Insert({"insert into project_supervisor (project_no,section_ids,amount,create_time,user_id,flow_no,rate,winning_amount) " +
            "values (#{projectNo},#{sectionIds},#{amount},now(),#{userId},#{flowNo},#{rate},#{winningAmount})"})
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer add(ProjectSupervisor projectSupervisor);

    @Delete({"delete from project_supervisor where id=#{id}"})
    Integer delete(Integer id);

    @Update({"<script>" +
            "update project_supervisor " +
            "<set>" +
            "<if test='projectNo != null and projectNo.length &gt; 0'>" +
            "project_no = #{projectNo}, " +
            "</if>" +
            "<if test='sectionIds != null '>" +
            "section_ids = #{sectionIds}, " +
            "</if>" +
            "<if test='amount != null '>" +
            "amount = #{amount}, " +
            "</if>" +
            "<if test='userId != null '>" +
            "user_id = #{userId}, " +
            "</if>" +
            " update_time = now() " +
            "</set> where id = #{id}" +
            "</script>"})
    Integer update(ProjectSupervisor projectSupervisor);

    @Select({"select ps.*,p.name as projectName,af.app_status as appStatus," +
            " af.approval_steps_id as approvalStepsId,u.name as userName from project_supervisor ps " +
            "left join project p on ps.project_no = p.project_no " +
            "left join audit_flow af on ps.flow_no = af.flow_no " +
            " left join user u on af.applicant = u.id  "+
            "where ps.id = #{id}"})
    @ResultMap("map")
    ProjectSupervisorVO findById(Integer id);
}